﻿using DAL.comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public  class ProductDAL
    {
        private SqlCommand cmd;
        public ProductDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
        }
        public DataSet CategoryList()
        {
            cmd.CommandText = "SelectCategoryList";
            cmd.Parameters.Clear();
            DataSet ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }
        public bool ForEdit(SqlCommand cmd)
        {
            try
            {
                int num = SQLHelper.ExecuteSQL(cmd);
                if (num > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }/// <summary>
         /// 加载存储过程参数方法实现
         /// </summary>
         /// <param name="bo"></param>

        private void SetPara(Product bo)
        {
            cmd.Parameters.Add("@ProductID", SqlDbType.NVarChar, 6).Value = bo.ProductID;
            cmd.Parameters.Add("@ProductName", SqlDbType.NVarChar, 80).Value = bo.ProductName;
            cmd.Parameters.Add("@SpellingCode", SqlDbType.NVarChar, 20).Value = bo.SpellingCode;
            cmd.Parameters.Add("@BarCode", SqlDbType.NVarChar, 80).Value = bo.BarCode;
            cmd.Parameters.Add("@Special", SqlDbType.Char, 6).Value = bo.Special;
            cmd.Parameters.Add("@Unit", SqlDbType.NVarChar, 20).Value = bo.Unit;
            cmd.Parameters.Add("@Origin", SqlDbType.NVarChar, 20).Value = bo.Origin;
            cmd.Parameters.Add("@BankName", SqlDbType.NVarChar, 40).Value = bo.ContactInfo.BankName;
            cmd.Parameters.Add("@BankAccount", SqlDbType.NVarChar, 50).Value = bo.ContactInfo.BankAccount;
            cmd.Parameters.Add("@Contacter", SqlDbType.NVarChar, 20).Value = bo.ContactInfo.Contacter;
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 30).Value = bo.ContactInfo.Email;


        }/// <summary>
         /// 新增数据方法实现
         /// </summary>
         /// <param name="bo"></param>
         /// <returns></returns>
        public bool Insert(Customer bo)
        {
            cmd.CommandText = "InsertCategory";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }/// <summary>
         /// 数据修改方法
         /// </summary>
         /// <param name="bo"></param>
         /// <returns></returns>
        public bool Update(Customer bo)
        {
            cmd.CommandText = "UpdateCustomer";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }/// <summary>
         /// 数据删除方法
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
        public bool Delete(Customer bo)
        {
            cmd.CommandText = "DeleteCustomer";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@CustomerID", SqlDbType.NVarChar, 6).Value = bo.CustomerID;
            return ForEdit(cmd);
        }
        /// <summary>
        /// 根据传入的ID查找对应记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Customer FindByID(string id)
        {
            cmd.CommandText = "FindCustomerByID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@CustomerID", SqlDbType.NVarChar, 6).Value = id;
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            Customer bo = new Customer();
            while (read.Read())
            {
                bo.CustomerID = read["CustomerID"].ToString().Trim();//将当前循环的记录列名为CustomerID的数据值赋值给bo.categoryID
                bo.CustomerName = read["CustomerName"].ToString().Trim();
            }
            read.Close();//关闭读库
            return bo;
        }


        public List<Customer> GetList()
        {
            cmd.CommandText = "SelectCustomerList";//指定存储过程名称
            cmd.Parameters.Clear();//清空存储过程参数列表
            List<Customer> list = new List<Customer>();//定义泛型集合用于存储数据库返回数据
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            while (read.Read())
            {
                Customer bo = new Customer();//实例化Customer类，用于存储一条产品类型记录
                bo.CustomerID = read["CustomerID"].ToString().Trim();//将当前循环的记录列名为CategoryID的数据值赋值给bo.Category
                bo.CustomerName = read["CustomerName"].ToString().Trim();
                list.Add(bo);//将当前记录添加至泛型集合list中
            }
            read.Close();//关闭读库
            return list;
        }
    }
}
